<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="cn.school.mybatis.utils.MyBatisUtil" %>
<%@ page import="cn.school.mybatis.dao.AdminDao" %>
<%@ page import="cn.school.mybatis.pojo.Admin" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: zhaoyang
  Date: 2020/8/5
  Time: 6:31 下午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录操作</title>
</head>
<body>

<%
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    // 查看cookie
    Cookie []cookies=request.getCookies();
    String userName="";
    boolean flag=true;
    for (int i=0;cookies!=null&&i<cookies.length;i++){
        if (cookies[i].getName().equals("username")) {
            userName=cookies[i].getValue();
            flag=false;
            break;
        }
    }
    if (flag) {
        Cookie c = new Cookie("userName", username);
        c.setMaxAge(60 * 60 * 24 * 30);
        c.setPath("/");
        response.addCookie(c);
    }

    SqlSession sess = MyBatisUtil.getSqlSession();
    AdminDao adminDao = sess.getMapper(AdminDao.class);
    List<Admin> list = adminDao.searchAll();
    MyBatisUtil.close(sess);


    for (Admin admin : list) {
        if (username.equals(admin.getUsername()) && password.equals(admin.getPassword())) {
            session.setAttribute("USER_LOGIN",admin.getUsername());
            out.println("<script>alert('"+userName+"欢迎登录！');location.replace('welcome.jsp')</script>");
            return;
        }
    }
    out.println("<script>location.replace('loginFail.jsp')</script>");

%>


</body>
</html>
